package com.dews.course.utils;


import com.dews.course.domain.ro.LinearRegressionModelRo;
import org.apache.commons.math3.stat.regression.SimpleRegression;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 清风学Java
 * @version 1.0.0
 * @date 2025/2/4
 */
public class PredictRemainingUtil {

    /**
     * 线性回归预测模型
     *
     * @param student
     * @return
     */
    public static Map<Integer, Double> predictRemainingCredits(LinearRegressionModelRo student) {
        SimpleRegression regression = new SimpleRegression();
        List<Double> earnedCredits = student.getEarnedCreditsPerSemester();
        int totalSemesters = student.getTotalSemesters();

        for (int i = 0; i < earnedCredits.size(); i++) {
            regression.addData(i + 1, earnedCredits.get(i));
        }

        Map<Integer, Double> predictedCredits = new HashMap<>();
        for (int i = earnedCredits.size() + 1; i <= totalSemesters; i++) {
            double predictedCredit = regression.predict(i);
            predictedCredits.put(i, predictedCredit);
        }

        return predictedCredits;
    }
}
